package com.example.controller.mobilecontroller;

import com.example.commons.utils.JwtTokenService;
import com.example.config.ResponseVO;
import com.example.pojo.ParentBind;
import com.example.pojo.StuInfo;
import com.example.pojo.TeacherInfo;
import com.example.pojo.dto.InfoDTO;
import com.example.pojo.dto.UserParentInfoDTO;
import com.example.service.FamilyInfoService;
import com.example.service.StuInfoService;
import com.example.service.TeacherInfoService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/mobile")
public class MobileController {
    // 家长绑定幼儿
    @Resource
    private StuInfoService stuInfoService;

    @Resource
    private FamilyInfoService familyInfoService;
    @Resource
    private TeacherInfoService teacherInfoService;

    @Resource
    private JwtTokenService tokenService;

    @PostMapping("/bindChildren")
    public ResponseEntity<ResponseVO<String>> bindChildren(@RequestBody ParentBind parentBind) {
        // 判断该学生是否存在
        StuInfo stuByName = stuInfoService.getStuByName(parentBind.getStuName());
        if (Objects.isNull(stuByName)) {
            // 返回给用户
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "该学生不存在"));
        }
        // 首先判断绑定码是否有效
        if (stuInfoService.getStuByBindCode(parentBind.getStuBindingCode())) {
            // 绑定成功之后需要返回需要的token
            String s = tokenService.generateToken(parentBind.getStuName());
            // 将家长的信息录入到学生的家长信息中
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "绑定成功", s));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "绑定失败，请确认绑定码是否输入正确"));
    }

    @PostMapping("/bindTeacher")
    public ResponseEntity<ResponseVO<Object>> bindTeacher(@RequestBody TeacherInfo teacherParams) {
        // 根据教师姓名去搜索
        TeacherInfo teacherByName = teacherInfoService.getTeacherByName(teacherParams.getTeacherName());
        System.out.println(teacherByName);
        if (Objects.isNull(teacherByName)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "该教师不存在"));
        }
        // 通过绑定码判断
        if (teacherParams.getBindCode().equals(teacherByName.getBindCode())) {
            String s = tokenService.generateToken(teacherByName.getTeacherName());
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "绑定成功", s));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "绑定失败，请确认绑定码是否输入正确"));
    }

    @GetMapping("/getMobileUserInfo")
    public ResponseEntity<ResponseVO<UserParentInfoDTO>> getMobileUserInfo(@RequestHeader(value = "Authorization", required = false) String authorizationHeader) {
        if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
            // 如果Authorization Header不存在或者格式不正确，则返回401 Unauthorized
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        String token = authorizationHeader.substring(7); // 去掉 "Bearer " 部分
        // 获取用户的姓名
        String usernameFromToken = tokenService.getUsernameFromToken(token);
        // 先去家庭信息去找是否有这个名字
        UserParentInfoDTO userInfoDTO = familyInfoService.selectFamilyByStuName(usernameFromToken);
        System.out.println(userInfoDTO);
        if(Objects.isNull(userInfoDTO)){
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "获取失败，请稍后再试", null));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", userInfoDTO));
    }

    @GetMapping("/getMobileTeacherInfo")
    public ResponseEntity<ResponseVO<TeacherInfo>> getMobileTeacherInfo(@RequestHeader(value = "Authorization", required = false) String authorizationHeader) {
        if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
            // 如果Authorization Header不存在或者格式不正确，则返回401 Unauthorized
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        String token = authorizationHeader.substring(7); // 去掉 "Bearer " 部分
        // 获取用户的姓名
        String usernameFromToken = tokenService.getUsernameFromToken(token);
        // 先去家庭信息去找是否有这个名字
        TeacherInfo teacherByName = teacherInfoService.getTeacherByName(usernameFromToken);
        if (Objects.isNull(teacherByName)) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST, "获取失败，请稍后再试", null));
        }
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "获取成功", teacherByName));
    }
}
